<?php

class UseRedis
{
	public $host = '101.200.34.58';
	public $port = '7379';
	public $auth = 'cjzR97hjwDYoJO*1GONv6NolJa0yiez';


	/**
	 * 操作redis  单个key获取
	 * @param array $info
	 * @return bool|int|string
	 */
	public function getRedis($info = array())
	{
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);

			$redis->select($info['db']);
			$res = $redis->get($info['key']);
			return $res;
		}
		catch (Exception $e){

		}
		return 0;
	}

	/**
	 * redis中获取数据  List
	 * @param array $info
	 * @return mixed
	 */
	public function mGetRedis($info = array())
	{
		try{
			//连接redis
			$notFound = array();
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$res = $redis->mget($info['key']);
			foreach ($res as $key => $val) {
				if (empty($val) || $val == 'null') {
					$notFound[] = $info['key'][$key];
					unset($res[$key]);
				} else {
					$res['found'][] = $val;
				}
			}
			$res['notFound'] = $notFound;
			return $res;
		}
		catch (Exception $e){

		}
	}

	/**
	 * redis中存放数据  List
	 * @param array $info
	 * @return int
	 */
	public function mSetRedis($info = array())
	{
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);

			foreach ($info['info'] as $key => $val) {
				$redis->set($val['key'], $val['val']);
				//如果存在失效时间， 则添加失效时间
				if (!empty($val['exptime']) && $val['exptime'] != '') {
					$redis->expire($val['key'], $val['exptime']);
				}
			}
			return 0;
		}catch (Exception $e){

		}
	}

	/**
	 * 删除单个key
	 * @param $info
	 * @return int
	 */
	public function delRedis($info){
		try{
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$res = $redis->del($info['key']);
			return $res;
		}catch (Exception $e){

		}
		return 0;
	}

	/**
	 * 设置 key
	 * @param array $info
	 * @return int
	 */
	public function setRedis($info = array())
	{
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);

			$redis->select($info['db']);

			$redis->set($info['key'], $info['value']);

			//如果存在失效时间， 则添加失效时间
			if (!empty($info['exptime']) && $info['exptime'] != '') {
				$redis->expire($info['key'], $info['exptime']);
			}
		}catch (Exception $e){

		}
		return 0;
	}

	/**
	 * 批量获取keys
	 * @param $info
	 * @return bool|int|string
	 */
	public function getKeys($info){
		try{
			//连接redis
			$return = array();
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$res = $redis->keys($info['key']);
			$ret = $redis->mget($res);
			if (!$ret) {
				return false;
			}
			foreach ($ret as $key => $val) {
				$return[$res[$key]] = json_decode($val, true);
			}
			return json_encode($return);
		}
		catch (Exception $e){

		}
		return 0;
	}

	/**
	 * 操作集合 set进去
	 * @param $info
	 * @return int
	 */
	public function setSSet($info){
		try {
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$redis->zAdd($info['key'], $info['date'], $info['value']);
			$redis->close();
		} catch (Exception $e) {

		}
		return 0;
    }

	/**
	 * 获取有序集合
	 * @param array $info
	 * @return array
	 */
    public function getSSet($info = array()) {
    	$return = array();
	    try {
		    $redis = new Redis();
		    $redis->connect($this->host, $this->port);
		    $redis->auth($this->auth);
		    $redis->select($info['db']);
		    if (!isset($info['page']) || !isset($info['pageCount'])) {
			    $start = 0;
				$end = 0;
		    } else {
			    $start = ( $info['page'] - 1 ) * $info['pageCount'];
			    $end = $info['page'] * $info['pageCount'] - 1;
		    }
		    $res = $redis->zRevRange($info['key'], $start, $end, 'WITHSCORES');
		    $redis->close();
		    $return = $res;
	    } catch (Exception $e) {

	    }
	    return $return;
    }

	/**
	 * 删除有序集合
	 * @param array $info
	 * @return array
	 */
	public function delSSetByValue($info = array()) {
		$return = array();
		try {
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$redis->zRem($info['key'], $info['value']);
			$redis->close();
		} catch (Exception $e) {

		}
		return $return;
	}

	/**
	 * 删除有序集合
	 * @param array $info
	 * @return array
	 */
	public function delSSetByScore($info = array()) {
		$return = array();
		try {
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$redis->zRangeByScore($info['key'], $info['score'], $info['score']);
			$redis->close();
		} catch (Exception $e) {

		}
		return $return;
	}

	/**
	 * 增量添加
	 * @param array $info
	 * @return array
	 */
	public function addSSetScore($info = array()) {
		$return = array();
		try {
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$redis->zIncrBy($info['key'], $info['score'], $info['value']);
			$redis->close();
		} catch (Exception $e) {

		}
		return $return;
	}

	/**
	 * 获取有序集合
	 * @param array $info
	 * @return array
	 */
	public function getListByKeys($info = array()) {
		$return = array();
		try {
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			foreach ($info['keys'] as $val) {
				$res = $redis->lRange($val, 0, 0);
				if (!empty($res)) {
					$return[$val] = json_decode($res[0], true);
				}
			}
			$redis->close();
		} catch (Exception $e) {

		}
		return $return;
	}

	/**
	 * 操作list
	 * @param $info
	 * @return int
	 */
	public function setList($info) {
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$redis->lPush($info['key'], $info['value']);
			$redis->close();
		}
		catch (Exception $e){

		}
		return 0;
	}

	/**
	 * 操作list 获取列表中元素大小
	 * @param $info
	 * @return array|void|bool|int
	 */
	public function getLsize($info) {
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$res = $redis->lLen($info['key']);
			$redis->close();
			return $res;
		}
		catch (Exception $e){

		}
		return 0;
	}


	/**
	 * 操作redis 批量获取， 从list 尾部获取
	 * page pageCount
	 * @param $info
	 * @return int|void|bool|array
	 */
	public function getLRange($info) {
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			$start = ( $info['page'] - 1 ) * $info['pageCount'];
			$end = $info['page'] * $info['pageCount'] - 1;
			$res = $redis->lRange($info['key'], $start, $end);
			$redis->close();
			return $res;
		}
		catch (Exception $e){

		}
		return 0;
	}


	public function ChangeRecoverToSend($info) {
		$return = array();
		try{
			//连接redis
			$redis = new Redis();
			$redis->connect($this->host, $this->port);
			$redis->auth($this->auth);
			$redis->select($info['db']);
			if ($info['key1'] == $info['key2'] || empty($info['key1']) || empty($info['key2'])) {
				return $return;
			}
			$loop = 0;
			while($redis->rpoplpush($info['key1'], $info['key2'])) {
				if (++$loop > 1000) {
					break;
				}
			}
		}
		catch (Exception $e){

		}
		return $return;
	}

}

//
//$redis = new UseRedis();
//$data = array(
//	'db'    => 0,
//	'key'   => 'chatListNeedReceived:461199585',
//	'value' => '18372551560',
//	'score' => 1
//);
//$qa = $redis->addSSetScore($data);
//var_dump($qa);



